using System;
using ConsentManagementProviderLib.Enum;
using UnityEngine;

namespace ConsentManagementProviderLib.Android
{
	internal class AndroidJavaConstruct
	{
		private static readonly string androidPluginName = "com.sourcepoint.cmplibrary.creation.FactoryKt";

		private AndroidJavaClass pluginFactoryClass;

		internal static AndroidJavaObject GetActivity()
		{
			return new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
		}

		internal AndroidJavaObject ConsrtuctLib(AndroidJavaObject spConfig, AndroidJavaObject activity, SpClientProxy spClient)
		{
			AndroidJavaObject result = pluginFactoryClass.CallStatic<AndroidJavaObject>("makeConsentLib", new object[3] { spConfig, activity, spClient });
			CmpDebugUtil.Log("consentLib is OK");
			return result;
		}

		internal AndroidJavaObject ConstructPrivacyManagerTab(PRIVACY_MANAGER_TAB tab)
		{
			AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.sourcepoint.cmplibrary.model.PMTab", CSharp2JavaStringEnumMapper.GetPrivacyManagerTabKey(tab), (int)tab);
			androidJavaObject.Set("key", CSharp2JavaStringEnumMapper.GetPrivacyManagerTabKey(tab));
			CmpDebugUtil.Log("PMTab is OK");
			return androidJavaObject;
		}

		internal AndroidJavaObject ConstructCampaign(AndroidJavaObject campaignType, AndroidJavaObject targetingParams, CAMPAIGN_TYPE campaignTypeForLog)
		{
			AndroidJavaObject result = new AndroidJavaObject("com.sourcepoint.cmplibrary.model.exposed.SpCampaign", campaignType, targetingParams);
			CmpDebugUtil.Log($"Campaign {campaignTypeForLog} is OK");
			return result;
		}

		internal AndroidJavaObject ConstructCampaignType(CAMPAIGN_TYPE campaignType)
		{
			AndroidJavaObject result = null;
			AndroidJavaClass androidJavaClass = new AndroidJavaClass("com.sourcepoint.cmplibrary.exception.CampaignType");
			switch (campaignType)
			{
			case CAMPAIGN_TYPE.GDPR:
				result = androidJavaClass.GetStatic<AndroidJavaObject>("GDPR");
				break;
			case CAMPAIGN_TYPE.CCPA:
				result = androidJavaClass.GetStatic<AndroidJavaObject>("CCPA");
				break;
			default:
				CmpDebugUtil.LogError("CampaignType is NULL. How did you get there?");
				break;
			}
			CmpDebugUtil.Log($"CampaignType {campaignType} is OK");
			return result;
		}

		internal AndroidJavaObject ConstructMessageLanguage(MESSAGE_LANGUAGE lang)
		{
			AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.sourcepoint.cmplibrary.model.MessageLanguage", CSharp2JavaStringEnumMapper.GetMessageLanguageKey(lang), (int)lang);
			androidJavaObject.Set("value", CSharp2JavaStringEnumMapper.GetMessageLanguageKey(lang));
			CmpDebugUtil.Log("MessageLanguage is OK");
			return androidJavaObject;
		}

		internal AndroidJavaObject ConstructSpConfig(int accountId, int propertyId, string propertyName, long messageTimeout, AndroidJavaObject language, CAMPAIGN_ENV campaignsEnvironment, AndroidJavaObject[] spCampaigns)
		{
			AndroidJavaObject result;
			using (AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.sourcepoint.cmplibrary.creation.SpConfigDataBuilder"))
			{
				androidJavaObject.Call<AndroidJavaObject>("addAccountId", new object[1] { accountId });
				CmpDebugUtil.Log("addAccountId is OK");
				androidJavaObject.Call<AndroidJavaObject>("addPropertyId", new object[1] { propertyId });
				CmpDebugUtil.Log("addPropertyId is OK");
				androidJavaObject.Call<AndroidJavaObject>("addPropertyName", new object[1] { propertyName });
				CmpDebugUtil.Log("addPropertyName is OK");
				androidJavaObject.Call<AndroidJavaObject>("addMessageLanguage", new object[1] { language });
				CmpDebugUtil.Log("addMessageLanguage is OK");
				androidJavaObject.Call<AndroidJavaObject>("addMessageTimeout", new object[1] { messageTimeout });
				CmpDebugUtil.Log("addMessageTimeout is OK");
				AndroidJavaObject androidJavaObject2 = ConstructCampaignEnv(campaignsEnvironment);
				androidJavaObject.Call<AndroidJavaObject>("addCampaignsEnv", new object[1] { androidJavaObject2 });
				CmpDebugUtil.Log("addCampaignsEnv is OK");
				foreach (AndroidJavaObject androidJavaObject3 in spCampaigns)
				{
					androidJavaObject.Call<AndroidJavaObject>("addCampaign", new object[1] { androidJavaObject3 });
					CmpDebugUtil.Log("addCampaign is OK");
				}
				result = androidJavaObject.Call<AndroidJavaObject>("build", Array.Empty<object>());
				CmpDebugUtil.Log("build() is OK");
			}
			CmpDebugUtil.Log("SpConfig is OK");
			return result;
		}

		internal AndroidJavaObject ConstructTargetingParam(string key, string value)
		{
			AndroidJavaObject result = new AndroidJavaObject("com.sourcepoint.cmplibrary.model.exposed.TargetingParam", key, value);
			CmpDebugUtil.Log("TargetingParam is OK");
			return result;
		}

		private AndroidJavaObject ConstructCampaignEnv(CAMPAIGN_ENV environment)
		{
			AndroidJavaObject androidJavaObject = new AndroidJavaObject("com.sourcepoint.cmplibrary.data.network.util.CampaignsEnv", CSharp2JavaStringEnumMapper.GetCampaignEnvKey(environment), (int)environment);
			androidJavaObject.Set("env", CSharp2JavaStringEnumMapper.GetCampaignEnvKey(environment));
			CmpDebugUtil.Log("campaignEnv is OK");
			return androidJavaObject;
		}

		internal void Dispose()
		{
			pluginFactoryClass = null;
		}
	}
}
